﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Browser;

namespace EditorialAdministration
{
    public partial class IconControl : UserControl
    {
        public IconControl()
        {
            if (SelectedControl == null)
                SelectedControl = this;
            // Required to initialize variables
            InitializeComponent();
            Loaded += new RoutedEventHandler(IconControl_Loaded);
        }

        void IconControl_Loaded(object sender, RoutedEventArgs e)
        {
            VisualStateManager.GoToState(SelectedControl, "Selected", true);
        }

        public static IconControl SelectedControl { get; set; }

        public static readonly DependencyProperty AppNameProperty = DependencyProperty.Register("AppName", typeof(string), typeof(IconControl), new PropertyMetadata(AppNameChanged));

        public static void AppNameChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            IconControl control = (IconControl)sender;
            control.lblName.Text = (string)e.NewValue;

        }
        public string AppName
        {
            get
            {
                return (string)this.GetValue(IconControl.AppNameProperty);
            }
            set
            {
                this.SetValue(IconControl.AppNameProperty, value);
            }
        }

        private void UserControl_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            // TODO: Add event handler implementation here.
            if (SelectedControl == this)
                return;
            VisualStateManager.GoToState(this, "MouseOver", true);
        }

        private void UserControl_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (SelectedControl == this)
                return;
            VisualStateManager.GoToState(this, "Base", true);
        }

        private void UserControl_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
        	// TODO: Add event handler implementation here
            if (SelectedControl != null)
            {
                VisualStateManager.GoToState(SelectedControl, "Base", true);
            }
            VisualStateManager.GoToState(this, "Selected", true);
            SelectedControl = this;

            string script = string.Format(@"UpdateAppDetails('{0}');", this.AppName);
            HtmlPage.Window.Eval(script);
        }

        private void Image_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
        	// TODO: Add event handler implementation here.
            Uri appUri;
            Uri.TryCreate(HtmlPage.Document.DocumentUri, "Application.aspx", out appUri);
            HtmlPage.Window.Navigate(appUri);
        }
    }
}